package com.wechat.core.exception;

import com.wechat.core.bean.Result;
import com.wechat.core.constant.enums.support.RStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * <p>title:</p>
 * <p>description:</p>
 * <p>company:</p>
 * <p></p>
 * 业务处理异常
 * service 通用
 *
 * @author <a href="jerry@kingyon.com">Peng yi</a>
 * @date 2018/12/10
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
public final class ServiceException extends RuntimeException {
    private static final long serialVersionUID = -1L;

    private Object data;

    private int code = RStatus.FAIL.getValue();

    public ServiceException() {
        super("服务异常");
    }

    public ServiceException(String message) {
        super(message);
    }

    public ServiceException(String message, int code) {
        super(message);
        this.code = code;
    }

    public ServiceException(String message, Object data) {
        super(message);
        if(data instanceof RStatus){
            RStatus status = (RStatus) data;
            this.code = status.getValue();
        }
        this.data = data;
    }

    public ServiceException(String message, Object data, int code) {
        super(message);
        this.data = data;
        this.code = code;
    }

    public ServiceException(Result result) {
        super(result.getMessage());
        this.data = result.getResult();
        this.code = result.getCode();
    }

    public ServiceException(Throwable cause) {
        super(cause);
    }

    public ServiceException(String message, Throwable cause) {
        super(message, cause);
    }

    public ServiceException(String message, Object data, Throwable cause) {
        super(message, cause);
        this.data = data;
    }

    @Override
    public String toString() {
        return "ServiceException{" +
                "data=" + data +
                ", code=" + code +
                ", message=" + this.getMessage() +
                '}';
    }
}
